//
//  RecommendViewController.swift
//  StudyDY
//
//  Created by yinchangming on 2018/7/4.
//  Copyright © 2018年 yinchangming. All rights reserved.
//

import UIKit

//首页-推荐
 let kItemMargin:CGFloat=10 //行距
 let kItemWidth=(kScreenW - 3*kItemMargin)/2 //item的宽度
 let kItemHeight=kItemWidth * 3 / 4 //item的高度是宽度的四分之三 （常规sell）
 let kItemFaceSroceHeight=kItemWidth * 4 / 3//item的高度是宽度的三分之四 （FaceSrocesell 颜值cell的item高度）

private let kLunBoH=kScreenW * 3 / 8
 let kGameH:CGFloat=90

 let kHeaderViewH:CGFloat=40//分组高度
 let kNomorlD="kNomorlD"
private let kFaceSocreID="kFaceSocreID"
 let kHeaderID="kHeaderID"

class RecommendViewController: BaseViewController {
    // 网络请求对象
    private lazy var requstModel:RecommendViewModel=RecommendViewModel()
    //轮播图对象
    private lazy var lunBoView = { () ->LunBoView in
        let lunbo=LunBoView.creatView()
        lunbo.frame=CGRect(x: 0, y: -(kLunBoH+kGameH), width: kScreenW, height: kLunBoH)
        return lunbo
    }()
    //推荐游戏
    private lazy var gameView={ () -> GameView in
            let gView=GameView.creatView()
            gView.frame=CGRect(x: 0, y: -kGameH, width: kScreenW, height: kGameH)
            return gView
    }()
    lazy var uicollectionView={ ()-> UICollectionView in
        //1 创建布局
        let layout=UICollectionViewFlowLayout()
        layout.itemSize=CGSize(width: kItemWidth, height: kItemHeight)
        layout.minimumLineSpacing=0
        layout.minimumInteritemSpacing=kItemMargin
        // 设置头部的大小
        layout.headerReferenceSize=CGSize(width: kScreenW, height: kHeaderViewH)
        //设置item的左右编剧
        layout.sectionInset=UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)
       
        
        
        
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.dataSource=self
        collectionView.delegate=self
        
        collectionView.showsVerticalScrollIndicator=false
        //注册普通cell
        collectionView.register(UINib(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: kNomorlD)
        //注册颜值cell
        collectionView.register(UINib(nibName: "CollectionFaceScoreViewCell", bundle: nil), forCellWithReuseIdentifier: kFaceSocreID)
        //注册头部cell
        collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderID)
        collectionView.backgroundColor=UIColor.white
        return collectionView
    }()
    override func viewDidLoad() {
        contentView=uicollectionView
        updateUI()
        //设置 uicollectionView 更随着父控件的宽度 高度变化而变化
        uicollectionView.autoresizingMask=[.flexibleHeight,.flexibleWidth]
        super.viewDidLoad()
        
    }
}
//界面相关
extension RecommendViewController{
    func updateUI(){
        //请求数据
        
        requstModel.requsedLunBo {
            self.lunBoView.lunbos=self.requstModel.lunbos
       
        }
        requstModel.request {
            self.uicollectionView.reloadData()
            //给推荐游戏 推数据
            self.gameView.isFromFrist=true
            self.gameView.anchor = self.requstModel.anchorCatgary
            self.finshRequst()
        }
        
        view.addSubview(uicollectionView)
        uicollectionView.addSubview(lunBoView)
        uicollectionView.addSubview(gameView)
        uicollectionView.contentInset = UIEdgeInsets(top: kLunBoH + kGameH, left: 0, bottom: 0, right: 0)
    }
}
//数据源相关
extension RecommendViewController : UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    //返回多少组
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return requstModel.anchorCatgary.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //获取 分组下面的房间个数
        return  requstModel.anchorCatgary[section].roomListResult.count
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //1获取组数据
        let grounp = requstModel.anchorCatgary[indexPath.section]
        // 2 获取组里面的roomlist数据
        let room = grounp.roomListResult[indexPath.item]
        if(indexPath.section==1)// 第一组为颜值  去获取FaceSroceCell
        {
            let  faseScrolCell=collectionView.dequeueReusableCell(withReuseIdentifier: kFaceSocreID, for: indexPath) as! CollectionFaceScoreViewCell
            faseScrolCell.room=room
            return faseScrolCell
        }else {//其他的正常获取常规cell
            let  normalCell=collectionView.dequeueReusableCell(withReuseIdentifier: kNomorlD, for: indexPath) as! CollectionNormalCell
            normalCell.room=room
            return normalCell
        }
        
    }
    //分组的头部
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let reusableView =  collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderID, for: indexPath) as! CollectionHeaderView
        let catGatgary =  requstModel.anchorCatgary[indexPath.section]
        reusableView.anchor=catGatgary
        return reusableView
    }
    
    // 根据不同组返回对应的item尺寸
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if(indexPath.section==1){//颜值
            return CGSize(width: kItemWidth, height: kItemFaceSroceHeight)
        }
        //其他情况
        return CGSize(width: kItemWidth, height: kItemHeight)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let type = self.requstModel.anchorCatgary[indexPath.section].roomListResult[indexPath.item].isVertical
//        type == 0 ? toShowPicRoom() : toShowPhoneRoom()
        toShowPicRoom() 
    }
    func toShowPhoneRoom(){
        let phoneRoom=RoomPhoneViewController()
      present(phoneRoom, animated: true, completion: nil)
    }
    func toShowPicRoom(){
        let picRoom=RoomComputerViewController()
        navigationController?.pushViewController(picRoom, animated: true)
    }
}
